package com.zbkj.admin.controller;

import com.zbkj.common.response.CommonResult;
import com.zbkj.common.vo.ChunkUploadVo;
import com.zbkj.common.vo.ChunkUploadResultVo;
import com.zbkj.service.service.BigFileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 大文件上传控制器
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/bigfile")
@Api(tags = "大文件上传")
public class BigFileUploadController {

    @Autowired
    private BigFileUploadService bigFileUploadService;

    /**
     * 检查文件是否已存在（断点续传）
     */
    @ApiOperation(value = "检查文件是否已存在")
    @GetMapping("/check")
    public CommonResult<ChunkUploadResultVo> checkFileExists(
            @ApiParam(name = "identifier", value = "文件唯一标识符", required = true)
            @RequestParam String identifier) {
        return CommonResult.success(bigFileUploadService.checkFileExists(identifier));
    }

    /**
     * 上传文件分片
     */
    @ApiOperation(value = "上传文件分片")
    @PostMapping("/chunk")
    public CommonResult<ChunkUploadResultVo> uploadChunk(
            @ApiParam(name = "file", value = "分片文件", required = true)
            @RequestParam("file") MultipartFile file,
            @ApiParam(name = "identifier", value = "文件唯一标识符", required = true)
            @RequestParam String identifier,
            @ApiParam(name = "filename", value = "文件名", required = true)
            @RequestParam String filename,
            @ApiParam(name = "chunkNumber", value = "当前分片索引", required = true)
            @RequestParam Integer chunkNumber,
            @ApiParam(name = "chunkSize", value = "分片大小", required = true)
            @RequestParam Long chunkSize,
            @ApiParam(name = "currentChunkSize", value = "当前分片实际大小", required = true)
            @RequestParam Long currentChunkSize,
            @ApiParam(name = "totalSize", value = "文件总大小", required = true)
            @RequestParam Long totalSize,
            @ApiParam(name = "totalChunks", value = "总分片数", required = true)
            @RequestParam Integer totalChunks,
            @ApiParam(name = "type", value = "文件类型")
            @RequestParam(required = false) String type,
            @ApiParam(name = "model", value = "模块名称", required = true)
            @RequestParam String model,
            @ApiParam(name = "pid", value = "分类ID", required = true)
            @RequestParam Integer pid,
            @ApiParam(name = "md5", value = "文件MD5值")
            @RequestParam(required = false) String md5) throws IOException {

        ChunkUploadVo chunkUploadVo = new ChunkUploadVo();
        chunkUploadVo.setIdentifier(identifier);
        chunkUploadVo.setFilename(filename);
        chunkUploadVo.setChunkNumber(chunkNumber);
        chunkUploadVo.setChunkSize(chunkSize);
        chunkUploadVo.setCurrentChunkSize(currentChunkSize);
        chunkUploadVo.setTotalSize(totalSize);
        chunkUploadVo.setTotalChunks(totalChunks);
        chunkUploadVo.setType(type);
        chunkUploadVo.setModel(model);
        chunkUploadVo.setPid(pid);
        chunkUploadVo.setMd5(md5);

        return CommonResult.success(bigFileUploadService.uploadChunk(file, chunkUploadVo));
    }

    /**
     * 合并文件分片
     */
    @ApiOperation(value = "合并文件分片")
    @PostMapping("/merge")
    public CommonResult<ChunkUploadResultVo> mergeChunks(
            @ApiParam(name = "identifier", value = "文件唯一标识符", required = true)
            @RequestParam String identifier,
            @ApiParam(name = "filename", value = "文件名", required = true)
            @RequestParam String filename,
            @ApiParam(name = "totalChunks", value = "总分片数", required = true)
            @RequestParam Integer totalChunks,
            @ApiParam(name = "totalSize", value = "文件总大小", required = true)
            @RequestParam Long totalSize,
            @ApiParam(name = "type", value = "文件类型")
            @RequestParam(required = false) String type,
            @ApiParam(name = "model", value = "模块名称", required = true)
            @RequestParam String model,
            @ApiParam(name = "pid", value = "分类ID", required = true)
            @RequestParam Integer pid,
            @ApiParam(name = "md5", value = "文件MD5值")
            @RequestParam(required = false) String md5) throws IOException {

        ChunkUploadVo chunkUploadVo = new ChunkUploadVo();
        chunkUploadVo.setIdentifier(identifier);
        chunkUploadVo.setFilename(filename);
        chunkUploadVo.setTotalChunks(totalChunks);
        chunkUploadVo.setTotalSize(totalSize);
        chunkUploadVo.setType(type);
        chunkUploadVo.setModel(model);
        chunkUploadVo.setPid(pid);
        chunkUploadVo.setMd5(md5);

        return CommonResult.success(bigFileUploadService.mergeChunks(chunkUploadVo));
    }

    /**
     * 获取上传进度
     */
    @ApiOperation(value = "获取上传进度")
    @GetMapping("/progress")
    public CommonResult<ChunkUploadResultVo> getUploadProgress(
            @ApiParam(name = "identifier", value = "文件唯一标识符", required = true)
            @RequestParam String identifier) {
        return CommonResult.success(bigFileUploadService.getUploadProgress(identifier));
    }

    /**
     * 取消上传
     */
    @ApiOperation(value = "取消上传")
    @DeleteMapping("/cancel")
    public CommonResult<String> cancelUpload(
            @ApiParam(name = "identifier", value = "文件唯一标识符", required = true)
            @RequestParam String identifier) {
        bigFileUploadService.cancelUpload(identifier);
        return CommonResult.success("上传已取消");
    }
} 